/*
 * @Author: Jim <jimjiang0909@gmail.com>
 * @Create time:   2016-07-22 11:20:28
 * @Last Modified by:   Jim
 * @Last Modified time: 2016-10-17 14:38:05
 * @File Name: browse.js
 */

import React, {
    Component,
} from 'react';
import {
    StyleSheet,
    View,
    ScrollView,
} from 'react-native';
import Filter from './filter';
import UserList from '../common/userList';

const needFile = ['search_photo', 'match_gender', 'match_smoker', 'match_ethnicity', 'match_disability', 
                  'match_age_min', 'match_age_max', 'r_country', 'match_r_state_id', 'is_distance', 'distance', 
                  'offset', 'page_num', 'search_type'];

export default class Browse extends Component {
    constructor(props) {
        super(props);
        this.state = {
            canRefresh: false,
        };

        SceneObj.Search = this;
    }
    
    notificationNavSwipe(type) {
        if(type == 'start') {
            this.setState({canRefresh: false});
        } else {
            this.setState({canRefresh: true});
        }
    }

    responseRender(type) {
        userObj = this.getUserInfo();
        this.forceUpdate();
    }

    shouldComponentUpdate(props, states) {
        return states.canRefresh ? true: false;
    }

    componentDidMount() {
        if(Store.lastSearchOptions) {
            Store.lastSearchOptions.page_num = 0;
            this.filterSearchOptions();
            this.setState({options: Store.lastSearchOptions});
        } else {
            this.getLastSearch();
        }  
    }

    render() {
        if(_.isEmpty(this.state.options)) {
           return (
                <View style={styles.container}>
                    {Utils.loading()}
                </View>
            ); 
        }

        return (
            <View style={styles.container}>
             <View style={styles.content}>
                <ScrollView
                    pagingEnabled={true}
                    scrollEventThrottle={1}
                    horizontal={true}
                    showsHorizontalScrollIndicator={false}
                    onScroll={(e)=> console.log(e)}
                    scrollEnabled={false}
                    onScrollAnimationEnd={(e) => console.log('end')}>
                    <UserList key={'browse'} api={'match_filter'} options={this.state.options} navigator={this.props.navigator} />
                </ScrollView>
                </View>
            </View>
        )
    }

    filterSearchOptions() {
        for(let i in Store.lastSearchOptions) {
            if (typeof Store.lastSearchOptions[i] == 'undefined' || Store.lastSearchOptions[i] === ''|| _.indexOf(needFile, i) == -1) {
                delete Store.lastSearchOptions[i];
            }
        }
    }

    getLastSearch() {
        let tthis = this;
        Share.fetchMain({
            url: 'get_last_search',
            callback: function (data) {
                tthis.state.options = data;
                tthis.state.options.page_num = 0;
                tthis.state.options.offset = 20;                
                Store.lastSearchOptions = tthis.state.options;
                tthis.setState(tthis.state);
            }
        })
    }

    rightButton() {
        Utils.navJumpTo(this.props.navigator,
            {
                name: "Filter",
                component: Filter,
                title: 'Filter',
                RightButton: 'done',
            }
        )
    }
}

const styles = StyleSheet.create(Css.BrowseStyle);